// pages/orderListView/orderListView.js

var Api = require('../../config.js');
let locationKey = "NC6BZ-ULNK3-VZL3O-YUAWK-OJRA7-EWF6Z"
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
var qqmapsdk;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    allOrderList: [

    ],

    leftFilterArray: [
      { name: "出发地", isSel: true, isMultipe: false, array: [] },
      { name: "目的地", isSel: false, isMultipe: false, array: [] },
      { name: "循环类型", isSel: false, isMultipe: false, array: [] },
      // { name: "出发地", isSel: false, isMultipe: false, array: [] },
    ],



    cityList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self = this
    self.getAllRouteList()

    // 实例化API核心类
    qqmapsdk = new QQMapWX({
      key: locationKey
    });
    self.getCityList()

  },

  sendMessage: function (e) {
    let self = this
    let postUrl = Api.service.postOrderMessage
    let app = getApp()
    let requestHead = app.globalData.requestHead
    if (requestHead.isHasHeadInfo == false) {
      app.selfLogin(self.sendMessage)
    }
    let head = requestHead.header

    var para = {
      driver_route_id: 1,
      passenger_route_id: 2,
      driver_id: 3,
      passenger_id: 4,
      seat: 5,
      price: 6,
      total: 7
    }

    wx.request({
      url: postUrl,
      data: para,
      header: head,
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      success: function (res) {
        let toastStr = res.data.msg
        wx.showToast({
          icon: 'success',
          title: toastStr,
        })
      }
    })



  },





  pySegSort: function (arr, empty) {


    if (!String.prototype.localeCompare)
      return null;

    var letters = "*ABCDEFGHJKLMNOPQRSTWXYZ".split('');
    var zh = "啊把差大额发噶哈级卡啦吗那哦爬器然啥他哇西呀咋".split('');


    var segs = []
    var curr = []
    letters.forEach(function (item, i) {
      curr = { letter: item, data: [] };
      arr.forEach(function (item2) {
        if ((!zh[i - 1] || zh[i - 1].localeCompare(item2.fullname, 'zh-Hans-CN', { sensitivity: 'base' }) <= 0) && item2.fullname.localeCompare(zh[i], 'zh-Hans-CN', { sensitivity: 'base' }) == -1) {
          curr.data.push(item2.fullname)
        }
      });
      if (curr.data.length) {
        segs.push(curr);
        curr.data.sort(function (a, b) {
          return a.localeCompare(b, 'zh-Hans-CN', { sensitivity: 'accent' });
        });
      }
    });

    var leftFilterArray = this.data.leftFilterArray
    leftFilterArray[0].array = segs
    leftFilterArray[1].array = segs


    this.setData({ cityList: segs, leftFilterArray: leftFilterArray })

  },


  getCityList: function () {
    let self = this

    // 调用接口
    qqmapsdk.getCityList({
      success: function (res) {
        let cityList = res.result[1]
        console.log(res)
        cityList = cityList.sort(function (a, b) {
          return (a.fullname).localeCompare((b.fullname), 'zh-Hans-CN', { sensitivity: 'accent' })

        });
        self.pySegSort(cityList, true)
        // self.setData({ cityList: cityList })
      },
      fail: function (res) {
        console.log(res);
      },
      complete: function (res) {
        // console.log(res);
      }
    });

    // 调用接口
    // qqmapsdk.getSuggestion({
    //   keyword: '技术',
    //   success: function (res) {
    //     console.log('技术1', res);
    //   },
    //   fail: function (res) {
    //     console.log('技术2', res);
    //   },
    //   complete: function (res) {
    //     console.log('技术3', res);
    //   }
    // });


  },






  //获取线路列表   getRouteList

  getAllRouteList: function () {
    let self = this
    let getUrl = Api.service.getRouteList
    let app = getApp()
    let requestHead = app.globalData.requestHead
    if (requestHead.isHasHeadInfo == false) {
      app.selfLogin(self.postInfoTapped)
    }
    let head = requestHead.header


    wx.request({
      url: getUrl,
      header: head,
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      success: function (res) {
        console.log("postres", res)
        let allOrderList = res.data.data
        self.setData({ allOrderList: allOrderList })
      }
    })
  },


  showFilterView: function () {





  },








  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  //实现绑定的formSubmit 将formId传给服务器
  formSubmit: function (e) {
    var that = this
    var app = getApp()
    let userOpenid = app.globalData.userInfo.openId
    wx.request({
      url: 'https://ihealth-wx.s1.natapp.cc/template',
      data: {
        'input': e.detail.value.input,
        'formId': e.detail.formId,
        'code': userOpenid
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        // success
        console.log('成功' + res);
        // console.log(e.detail.formId);
      },
      fail: function (err) {
        // fail
        console.log('失败' + err);
      },
      complete: function () {
        // complete
      }
    })
  },







  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})